// *** MAP VIEWER *** //
// Shows map to each other
//
// @project: GoldenShield
// @author: Kasper
// @version: 1.0

var mapViewer = {
    mapWith: 800,
    mapHeight: 600,
    space: 0,
    
    init: function() {
        this.mapArea = $("<div id='mapHandlerArea'></div>");
        $(this.mapArea).css({overflow: 'auto', width: '100%', height: '100%', backgroundColor: '#fff', zIndex: '1000', position: 'absolute'});
        $('body').append(this.mapArea);
        this.putMaps();
    },
    
    putMaps: function() {
      server.callXmapAPI('listMaps', function(maps) {
          if(maps && maps.length > 0) {
              for(var i in maps) {
                  mapViewer.putMap(maps[i]);
              }
          }
      }, null, 'json');  
    },
    
    putMap: function(mapFile) {
        var fileParts = mapFile.split('.');
        var cordinates = fileParts[0].split('_');
        
        if(cordinates.length > 0) {
        var posX = (cordinates[0] - 1) * this.mapWith + this.space ;
        var posY = (cordinates[1] - 1) * this.mapHeight + this.space;
        
        var mapContainer = $("<div class='mapContainer " + mapFile + "'></div>");
        $(mapContainer).css({left: posX, top: posY, position: 'absolute', width: '800', height: '600', border: '1px solid black'});
        
        server.callXmapAPI('loadMap', function(map) {
          $(mapContainer).html(map);
          $(mapViewer.mapArea).append(mapContainer);
        }, fileParts[0]);
        }
        
    }
}

mapViewer.init();